package com.msmall.community.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.Messages;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.common.OnlineUser;
import com.msmall.community.common.model.Commend;
import com.msmall.community.common.service.CommendManager;
import com.msmall.community.klg.service.KlgManager;
import com.msmall.community.question.service.QuestionManager;

public class CommendTag extends TagSupport{

	private static final long serialVersionUID = -2546013797744247966L;
	private static final Log logger = LogFactory.getLog(CommendTag.class);
	
	public static final String MESSAGE_COMMEND_SET = Messages.getMessage("common.bbs.topic.commend.set");
	public static final String MESSAGE_COMMEND_REMOVE = Messages.getMessage("common.bbs.topic.commend.remove");
	
	/**
	 * 推荐来源类型  ConstantDB.COMMEND_TYPE_
	 * 1:BBS 2Space 3问答 4知识
	 */
	private String type;
	
	private String id;
	
	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		String message = "";
		try {
			if(!isAuthorized()){
				return (SKIP_BODY);
			}
			Commend commend = getCommend();
			if(commend == null){
				if(Constants.USER_AGENT_TYPE_HTML.equalsIgnoreCase(getUserAgentType())){
					message = getCommend(ConstantUrl.HTML_COMMEND_SET,MESSAGE_COMMEND_SET,Constants.HTML_NOTATION_AND);
				}else{
					message = getCommend(ConstantUrl.WML_COMMEND_SET,MESSAGE_COMMEND_SET,Constants.WML_NOTATION_AND);
				}
			}else{
				if(Constants.USER_AGENT_TYPE_HTML.equalsIgnoreCase(getUserAgentType())){
					message = getCommend(ConstantUrl.HTML_COMMEND_REMOVE,MESSAGE_COMMEND_REMOVE,Constants.HTML_NOTATION_AND);
				}else{
					message = getCommend(ConstantUrl.WML_COMMEND_REMOVE,MESSAGE_COMMEND_REMOVE,Constants.WML_NOTATION_AND);
				}
			}
			writer.print(message);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		return (SKIP_BODY);
	}
	
	private boolean isAuthorized(){
		try {
			if(ConstantDB.COMMEND_TYPE_QUESTION.compareTo(Short.valueOf(type)) == 0){
				QuestionManager manager = (QuestionManager) ApplicationManager.appContext.getBean("questionManager");
				return manager.isAuthorizedCommend(getUserid());
			}
			if(ConstantDB.COMMEND_TYPE_KLG.compareTo(Short.valueOf(type)) == 0){
				KlgManager manager = (KlgManager) ApplicationManager.appContext.getBean("klgManager");
				return manager.isAuthorizedCommend(getUserid());
			}
		} catch (Exception e) {
			logger.debug(e);
		}
		return false;
	}
	private String getCommend(String url, String message, String and){
		String ctx = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		StringBuffer buffer = new StringBuffer();
		String path = ctx + url + "?type=" + type + and + "eid=" + id;
		buffer.append("<a href=\"").append(path).append("\" >").append(message).append("</a>");
		return buffer.toString();
	}
	
	private Commend getCommend(){
		try{
			CommendManager manager = (CommendManager) ApplicationManager.appContext.getBean("commendManager");
			return manager.getByTypeAndId(Short.valueOf(type), Long.valueOf(id));
		}catch(Exception e){
			logger.debug(e);
			return null;
		}
	}
	private Long getUserid(){
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpSession session = request.getSession();
		Long userid = (Long) session.getAttribute(Constants.SESSION_USER_ID_KEY);
		return userid;
	}
	private String getUserAgentType(){
		try{
			return OnlineUser.getOnlineUsers().get(getUserid()).getUserAgentBean().getUserAgentType();
		}catch(Exception e){
			return Constants.USER_AGENT_TYPE_HTML;
		}
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}
